{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4d42da0c-9eec-459a-a4cd-1e26eb8b42f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正常邮件的文件列表 ['normal-mail1.txt', 'normal-mail2.txt', 'normal-mail3.txt', 'normal-mail4.txt', 'normal-mail5.txt', 'normal-mail6.txt', 'normal-mail7.txt', 'normal-mail8.txt', 'normal-mail9.txt']\n",
      "垃圾邮件的文件列表 ['spam-mail1.txt', 'spam-mail2.txt', 'spam-mail3.txt', 'spam-mail4.txt', 'spam-mail5.txt', 'spam-mail6.txt', 'spam-mail7.txt', 'spam-mail8.txt', 'spam-mail9.txt']\n",
      "训练集中所有的有效词语列表：\n",
      "[[], ['期刊'], ['某某', '期刊'], ['期刊'], ['期刊'], ['期刊'], ['期刊'], ['期刊'], ['期刊'], [], ['小李', '你好'], ['李老师', '您好'], ['小李', '你好'], ['小张', '你好'], ['张老师', '您好'], ['李老师', '你好'], ['小张', '你好'], ['小张', '你好']]\n",
      "训练集中出现频率最高的前十个词语：\n",
      "['期刊', '你好', '小张', '小李', '李老师', '您好', '某某', '张老师']\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "normalFileList=os.listdir(r\"./item5/item5-ss-data/normal/\")\n",
    "spamFileList=os.listdir(r\"./item5/item5-ss-data/spam/\")\n",
    "print(\"正常邮件的文件列表\",normalFileList)\n",
    "print(\"垃圾邮件的文件列表\",spamFileList)\n",
    "stopList=[]\n",
    "for line in open(\"./item5/item5-ss-data/stopwords.txt\",encoding='utf-8'):\n",
    "    stopList.append(line[:len(line)-1])\n",
    "from jieba import cut\n",
    "from re import sub\n",
    "def getWords(file,stopList):\n",
    "    wordsList=[]\n",
    "    for line in open(file,encoding='utf-8'):\n",
    "        line=line.strip()\n",
    "        line=sub(r'[.【】0-9、————，。!\\~*]','',line)\n",
    "        line=cut(line)\n",
    "        line=filter(lambda word:len(word)>1,line)\n",
    "        wordsList.extend(line)\n",
    "        words=[]\n",
    "        for i in wordsList:\n",
    "            if i not in stopList and i.strip()!='' and i!=None:\n",
    "                words.append(i)\n",
    "        return words\n",
    "from collections import Counter\n",
    "from itertools import chain\n",
    "allwords=[]\n",
    "for spamfile in spamFileList:\n",
    "    words=getWords(\"./item5/item5-ss-data/spam/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "for normalfile in normalFileList:\n",
    "    words=getWords(\"./item5/item5-ss-data/normal/\"+normalfile,stopList)\n",
    "    allwords.append(words)\n",
    "print(\"训练集中所有的有效词语列表：\")\n",
    "print(allwords)\n",
    "frep=Counter(chain(*allwords))\n",
    "topTen=frep.most_common(10)\n",
    "topWords=[w[0] for w in topTen]\n",
    "print(\"训练集中出现频率最高的前十个词语：\")\n",
    "print(topWords)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "936ef3ae-ff8d-499b-8e78-9f48afc6ac6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集中出现频率最高的前十个词语de频率：\n",
      "[[0 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 1 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 1 0 1 0 0 0 0]\n",
      " [0 0 0 0 1 1 0 0]\n",
      " [0 1 0 1 0 0 0 0]\n",
      " [0 1 1 0 0 0 0 0]\n",
      " [0 0 0 0 0 1 0 1]\n",
      " [0 1 0 0 1 0 0 0]\n",
      " [0 1 1 0 0 0 0 0]\n",
      " [0 1 1 0 0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "vector=[]\n",
    "for words in allwords:\n",
    "    temp=list(map(lambda x:words.count(x),topWords))\n",
    "    vector.append(temp)\n",
    "vector=np.array(vector)\n",
    "print(\"训练集中出现频率最高的前十个词语de频率：\")\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaa27bf7-cfbc-4f4a-815b-8f6b1604108c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
